package com.ciwong.epaper.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.TimeoutError;
import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.common.EVariableGlobal;
import com.ciwong.epaper.common.dialogbottom.CatalogBottomDialog;
import com.ciwong.epaper.common.dialogbottom.ChackGradeBottomDialog;
import com.ciwong.epaper.modules.epaper.dao.EpaperDao;
import com.ciwong.epaper.modules.epaper.util.EpaperJumpManager;
import com.ciwong.epaper.modules.me.adapter.NewBookDeskAdapter;
import com.ciwong.epaper.modules.me.bean.BookDeskBean;
import com.ciwong.epaper.modules.me.bean.BookDeskCataloguesBean;
import com.ciwong.epaper.modules.me.bean.CatalogueUnitBean;
import com.ciwong.epaper.modules.me.bean.Clazz;
import com.ciwong.epaper.modules.me.bean.MaterialBean;
import com.ciwong.epaper.modules.me.bean.RefreshNewBookDesk;
import com.ciwong.epaper.modules.me.bean.SubjectBean;
import com.ciwong.epaper.modules.me.ui.MyServicesActivity;
import com.ciwong.epaper.modules.me.util.MeJumpManager;
import com.ciwong.epaper.util.BaseExtCallBack;
import com.ciwong.epaper.util.DoubleClickCheckUtils;
import com.ciwong.epaper.util.SerializableManager;
import com.ciwong.epaper.util.ViewUtil;
import com.ciwong.eventbus.EventBus;
import com.ciwong.libs.utils.NetworkUtils;
import com.ciwong.mobilelib.i.BaseCallBack;
import com.ciwong.mobilelib.ui.BaseActivity;
import com.ciwong.mobilelib.utils.ToastUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * Created by keepon on 2018/8/7.
 */
public class JiaoCaiTongBuItemView extends LinearLayout {
    private static final String TAG = "JiaoCaiTongBuItemView";
    private NewBookDeskAdapter mNewBookDeskAdapter;
    private TextView mTvTypeName;
    private RecyclerView mRecyclerView;
    private BookDeskCataloguesBean mBookDeskCatalogueBean = new BookDeskCataloguesBean();
    private List<SubjectBean> mSubjectBeanList = new ArrayList<>();
    private List<BookDeskCataloguesBean.CataloguesBean> mCataloguesBeanList = new ArrayList<>();
    private ArrayList<String> mVersionIdList = new ArrayList();

    private HashMap<String, CatalogueUnitBean> mShowingCatalogueUnitBeans = new HashMap();
    private HashMap<String, CatalogueUnitBean> mReadyShowCatalogueUnitBeans = new HashMap();
    private NewBookDeskAdapter.OnMaterItemClicklistener mOnMaterItemClicklistener;
    private ChackGradeBottomDialog chackGradeBottomDialog;
    private CatalogBottomDialog catalogBottomDialog;
    private Context mContext;
    private BookDeskBean mBookDeskBean;

    public JiaoCaiTongBuItemView(Context context) {
        this(context, null);
        mContext = context;
    }

    public JiaoCaiTongBuItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public JiaoCaiTongBuItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(this.getContext()).inflate(R.layout.layout_kewaituozhan_item, this);
        init();
        initEvent();

    }


    private void init() {
        SerializableManager.getInstance().deSerialize(SerializableManager.SerializeKey.SHARE_KEY_BOOK_DESK_CATALOGUE, new BaseCallBack() {
            @Override
            public void failed(int errorCode, Object data) {
                failed(data);
            }

            @Override
            public void failed(Object data) {
            }

            @Override
            public void success(Object data) {
                try {
                    if (data != null) {
                        mBookDeskCatalogueBean = (BookDeskCataloguesBean) data;
                        updateCataloguesBeanLocal(mBookDeskCatalogueBean);

                    }
                } catch (Exception e) {

                }
            }
        });

        mTvTypeName = findViewById(R.id.tv_type_name);
        mRecyclerView = findViewById(R.id.recycler_view);
        mRecyclerView.setFocusable(false);
        mTvTypeName.setText("教材同步");
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        mRecyclerView.addItemDecoration(new DividerItemDecorationLastHide(getContext(), LinearLayoutManager.VERTICAL, ViewUtil.dip2px(getContext(), (float) 0.5), R.color.def_line_color));
        mNewBookDeskAdapter = new NewBookDeskAdapter(getContext(), mSubjectBeanList, mCataloguesBeanList);
        mRecyclerView.setAdapter(mNewBookDeskAdapter);

        mNewBookDeskAdapter.setOnDialogCreatedListener(new NewBookDeskAdapter.OnDialogCreatedListener() {
            @Override
            public void onCreatedListener(Dialog dialog) {
                if (EVariableGlobal.CHECKED_FRAGMENT_BOOKDESK == true && dialog != null) {
                    EVariableGlobal.CHECKED_GUIDE_DIALOG_SHOWING = true;
                    dialog.show();
                }
            }
        });
        mOnMaterItemClicklistener = new NewBookDeskAdapter.OnMaterItemClicklistener() {
            @Override
            public void onClickChangeMoudle(final int mSubjectPosition, final SubjectBean subjectBean, final BookDeskCataloguesBean.CataloguesBean cataloguesBean, String currentUnitBeanUnitid) {

                if (cataloguesBean == null) {
                    return;
                }
                if (!DoubleClickCheckUtils.vertify500Duration()) {
                    return;
                }
                try {
                    catalogBottomDialog.initData(cataloguesBean.getData(), cataloguesBean.getVersionId());
                    catalogBottomDialog.setOnItemCallBackListener(new CatalogBottomDialog.ItemCallBack() {
                        @Override
                        public void onItemCallBack(CatalogueUnitBean returnCatalogueUnitBean) {
                            try {
                                String mBookIds;
                                List<MaterialBean> materialBeanList = subjectBean.getMaterial();
                                mBookIds = getBookIds(materialBeanList);
                                String packAgeId = "";
                                String resourceName = "";
                                if (returnCatalogueUnitBean != null) {
                                    packAgeId = returnCatalogueUnitBean.getUnitid();
                                    resourceName = returnCatalogueUnitBean.getName();
                                }
                                EpaperJumpManager.jumpToNewBookContentFromDesk(R.string.go_back, (Activity) mContext, mBookIds, packAgeId, resourceName, true);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            BookDeskCataloguesBean.CataloguesBean retrunCatalogueBean = mCataloguesBeanList.get(mSubjectPosition);
                            List<CatalogueUnitBean> catalogueUnitBeanList = retrunCatalogueBean.getData();
                            setSelectedCataloguesUnitBean(catalogueUnitBeanList, returnCatalogueUnitBean);
                            mBookDeskCatalogueBean.setCatalogues(mCataloguesBeanList);
                            updateCataloguesAdapterAndSaveLocal();
                        }
                    });
                    catalogBottomDialog.show();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onClickChangeMaterial(final int subjectPosition, SubjectBean subjectBean, BookDeskCataloguesBean.CataloguesBean cataloguesBean, final MaterialBean selectedMaterialBean) {

                if (!DoubleClickCheckUtils.vertify500Duration()) {
                    return;
                }
                if (!NetworkUtils.isOnline()) {
                    ToastUtil.INSTANCE.toastCenterNoNetError();
                    return;
                }
                try {
                    chackGradeBottomDialog.initData(subjectBean);
                    chackGradeBottomDialog.setOnItemCallBackListener(new ChackGradeBottomDialog.ItemCallBack() {
                        @Override
                        public void onItemCallBack(List<MaterialBean> materialBeans, int position) {
                            if (materialBeans != null) {
                                MaterialBean materialBean = materialBeans.get(position);
                                if (materialBean.getVersionId() == selectedMaterialBean.getVersionId()) {
                                    return;
                                }
                                Log.e(TAG, "onItemCallBack: " + materialBean.getVersionId() + "  " + materialBean.getVersionName());
                                getBookDeskItemCatalogue(materialBean.getVersionId() + "", subjectPosition, true, materialBean);
                            }
                        }
                    });
                    chackGradeBottomDialog.show();
                } catch (Exception e) {

                }

            }

            @Override
            public void onClickStartStudy(int position, SubjectBean subjectBean, BookDeskCataloguesBean.CataloguesBean cataloguesBean) {
                if (!DoubleClickCheckUtils.vertify500Duration()) {
                    return;
                }


                try {
                    String mBookIds;
                    List<MaterialBean> materialBeanList = subjectBean.getMaterial();
                    mBookIds = getBookIds(materialBeanList);
                    MaterialBean mMaterialBean = getMaterialBean(materialBeanList);
                    String packAgeId = "";
                    String resourceName = "";
                    List<CatalogueUnitBean> catalogueUnitBeanList = cataloguesBean.getData();
                    CatalogueUnitBean selectedCataloguesUnitBean = getSelectedCataloguesUnitBean(catalogueUnitBeanList);
                    if (selectedCataloguesUnitBean != null) {
                        packAgeId = selectedCataloguesUnitBean.getUnitid();
                        resourceName = selectedCataloguesUnitBean.getName();
                    }
//                    if (mMaterialBean.getIsExpire() == 1 || mMaterialBean.getIsFree() == 1) {
                    EpaperJumpManager.jumpToNewBookContentFromDesk(R.string.go_back, (Activity) mContext, mBookIds, packAgeId, resourceName, true);
//                    } else {
//                        MeJumpManager.jumpBuyService((Activity) mContext, R.string.go_back, mMaterialBean.getServerId(), -1, 1, "");
//                    }
                } catch (Exception e) {

                }

            }

            @Override
            public void onClickRefreshCatalogue(int subjectPosition, SubjectBean subjectBean, BookDeskCataloguesBean.CataloguesBean cataloguesBean) {
                if (mVersionIdList != null && mVersionIdList.size() > subjectPosition) {
                    getBookDeskItemCatalogue(mVersionIdList.get(subjectPosition), subjectPosition, false, null);

                }
            }


        };
        mNewBookDeskAdapter.setOnMaterItemClicklistener(mOnMaterItemClicklistener);

        initDialogFragment();
    }

    private void initEvent() {

    }

    //从本地获取科目教材选中情况刷新到页面
    private void updateMaterialItemBeansLocal(List<SubjectBean> subjectBeanList) {
        if (subjectBeanList == null) {
            return;
        }
        mSubjectBeanList.clear();
        mSubjectBeanList.addAll(subjectBeanList);
        try {
            //获取本地记录科目的VersionId,放入集合mVersionIdList，首页请求失败，点击刷新时需要的参数
            for (SubjectBean subjectBean : subjectBeanList) {
                List<MaterialBean> localMaterialBeanList = subjectBean.getMaterial();
                if (localMaterialBeanList != null) {
                    for (MaterialBean localMaterialBean : localMaterialBeanList) {
                        if (localMaterialBean.isSelected()) {
                            mVersionIdList.add(localMaterialBean.getVersionId() + "");
                        }
                    }
                }
            }
        } catch (Exception e) {

        }

        //教材同步刷新
        mNewBookDeskAdapter.updateSubjectBeanList(mSubjectBeanList);
//		mNewBookDeskAdapter.updateData(mSubjectBeanList, mCataloguesBeanList);

    }

    //从本地获取单元选中情况刷新到页面
    private void updateCataloguesBeanLocal(BookDeskCataloguesBean bookDeskCatalogueBean) {
        if (bookDeskCatalogueBean == null) {
            return;
        }
        mCataloguesBeanList = bookDeskCatalogueBean.getCatalogues();
        if (mCataloguesBeanList != null) {
            for (int i = 0; i < mCataloguesBeanList.size(); i++) {
                BookDeskCataloguesBean.CataloguesBean localCataloguesBean = mCataloguesBeanList.get(i);
                if (localCataloguesBean.getData() != null) {
                    setSelectedCataloguesUnitBeanLocal(localCataloguesBean.getData());

                }
            }
            mNewBookDeskAdapter.updateCatalogueList(mCataloguesBeanList);
        }
    }

    // //记录本地选中的单元
    private List<CatalogueUnitBean> setSelectedCataloguesUnitBeanLocal(List<CatalogueUnitBean> catalogueUnitBeanList) {
        try {
            if (catalogueUnitBeanList != null && catalogueUnitBeanList.size() > 0) {
                for (int i = 0; i < catalogueUnitBeanList.size(); i++) {
                    CatalogueUnitBean catalogueUnitBean = catalogueUnitBeanList.get(i);
                    if (catalogueUnitBean.getChildren() != null && catalogueUnitBean.getChildren().size() > 0) {
                        catalogueUnitBean.setChildren(setSelectedCataloguesUnitBeanLocal(catalogueUnitBean.getChildren()));
                    } else {
                        if (catalogueUnitBean.isSelected()) {
                            mShowingCatalogueUnitBeans.put(catalogueUnitBean.getUnitid(), catalogueUnitBean);
                        }
                    }
                }

            }
        } catch (Exception e) {

        }

        return catalogueUnitBeanList;
    }

    private void updateMaterialItemBeansFromNet(List<SubjectBean> subjectBeanList) {
        if (subjectBeanList == null) {
            return;
        }
        Clazz clazz = EApplication.getInstance().getClazz();
        if (clazz == null || clazz.getGradeId() == 0) {
            clazz = new Clazz();
            clazz.setGradeId(1107);
        }
        try {
            mVersionIdList.clear();
            boolean isSelected = false;
            for (int m = 0; m < subjectBeanList.size(); m++) {
                SubjectBean netSubjectBean = subjectBeanList.get(m);
                isSelected = false;
                List<MaterialBean> netMaterialBeans = netSubjectBean.getMaterial();
                if (netMaterialBeans != null && netMaterialBeans.size() > 0) {
                    for (int i = 0; i < netMaterialBeans.size(); i++) {
                        MaterialBean netMaterialBean = netMaterialBeans.get(i);
                        out:
                        //把本地选中的记录赋值给新拉到的数据
                        for (SubjectBean localSubject : mSubjectBeanList) {
                            List<MaterialBean> localMaterialBeans = localSubject.getMaterial();
                            for (int j = 0; j < localMaterialBeans.size(); j++) {
                                if (localMaterialBeans.get(j).getVersionId() == netMaterialBean.getVersionId()) {
                                    if (localMaterialBeans.get(j).isSelected()) {
                                        isSelected = true;
                                        mVersionIdList.add(netMaterialBean.getVersionId() + "");
                                    }
                                    netMaterialBean.setSelected(localMaterialBeans.get(j).isSelected());
                                    break out;
                                }
                            }
                        }

                    }

                    //本地没有记录，根据班级默认选一个
                    if (!isSelected) {
                        for (int i = 0; i < netMaterialBeans.size(); i++) {
                            MaterialBean netMaterialBean = netMaterialBeans.get(i);
                            if (netMaterialBean.getGradeId() == clazz.getGradeId()) {
                                netMaterialBean.setSelected(true);
                                isSelected = true;
                                mVersionIdList.add(netMaterialBean.getVersionId() + "");
                                break;
                            }
                        }
                    }
                    //对应班级没有,默认选第一个
                    if (!isSelected) {
                        if (netMaterialBeans != null && netMaterialBeans.size() > 0) {
                            MaterialBean netMaterialBean = netMaterialBeans.get(0);
                            netMaterialBean.setSelected(true);
                            isSelected = true;
                            mVersionIdList.add(netMaterialBean.getVersionId() + "");
                        }
                    }
                }

            }
        } catch (Exception e) {

        }
        mSubjectBeanList.clear();
        mSubjectBeanList.addAll(subjectBeanList);
        //教材同步刷新
        updateSubjectBeanListAdapterAndSaveLocal();

    }

    //把本地选中的单元数据同步到新获取到的网络数据中
    private boolean synSelectedCatalogueUnitBean(List<CatalogueUnitBean> catalogueUnitBeanList) {
        try {
            for (int j = 0; j < catalogueUnitBeanList.size(); j++) {
                CatalogueUnitBean netCatalogueUnitBean = catalogueUnitBeanList.get(j);
                if (netCatalogueUnitBean.getChildren() != null && netCatalogueUnitBean.getChildren().size() > 0) {
                    boolean b = synSelectedCatalogueUnitBean(netCatalogueUnitBean.getChildren());
                    if (b) {
                        return true;
                    }
                } else {
                    Set<Map.Entry<String, CatalogueUnitBean>> entries = mShowingCatalogueUnitBeans.entrySet();
                    for (Map.Entry<String, CatalogueUnitBean> showingCatalogueUnitBean : entries) {
                        if (showingCatalogueUnitBean.getKey().equals(netCatalogueUnitBean.getUnitid()) && showingCatalogueUnitBean.getValue().isSelected()) {
                            netCatalogueUnitBean.setSelected(true);
                            mReadyShowCatalogueUnitBeans.put(netCatalogueUnitBean.getUnitid(), netCatalogueUnitBean);
                            return true;
                        }
                    }
                }
            }
        } catch (Exception e) {

        }
        return false;

    }

    //单个科目切换教材时，如果此时切回的教材是页面初始化后切换过的，单元的记录选中情况可以保持
    private boolean synSelectedItemCatalogueUnitBean(List<CatalogueUnitBean> catalogueUnitBeanList) {
        try {
            for (int j = 0; j < catalogueUnitBeanList.size(); j++) {
                CatalogueUnitBean netCatalogueUnitBean = catalogueUnitBeanList.get(j);
                if (netCatalogueUnitBean.getChildren() != null && netCatalogueUnitBean.getChildren().size() > 0) {
                    boolean b = synSelectedCatalogueUnitBean(netCatalogueUnitBean.getChildren());
                    if (b) {
                        return true;
                    }
                } else {

                    Set<Map.Entry<String, CatalogueUnitBean>> entries = mShowingCatalogueUnitBeans.entrySet();
                    for (Map.Entry<String, CatalogueUnitBean> showingCatalogueUnitBean : entries) {
                        if (showingCatalogueUnitBean.getKey().equals(netCatalogueUnitBean.getUnitid()) && showingCatalogueUnitBean.getValue().isSelected()) {
                            netCatalogueUnitBean.setSelected(true);
                            return true;
                        }
                    }
                }
            }
        } catch (Exception e) {

        }
        return false;

    }

    //设置选中的CataloguesUnitBean
    private List<CatalogueUnitBean> setSelectedCataloguesUnitBean(List<CatalogueUnitBean> catalogueUnitBeanList, CatalogueUnitBean returnCatalogueUnitBean) {
        try {
            if (catalogueUnitBeanList != null && catalogueUnitBeanList.size() > 0) {
                for (int i = 0; i < catalogueUnitBeanList.size(); i++) {
                    CatalogueUnitBean catalogueUnitBean = catalogueUnitBeanList.get(i);
                    if (catalogueUnitBean.getChildren() != null && catalogueUnitBean.getChildren().size() > 0) {
                        catalogueUnitBean.setChildren(setSelectedCataloguesUnitBean(catalogueUnitBean.getChildren(), returnCatalogueUnitBean));
                    } else {
                        if (catalogueUnitBean.getUnitid().equals(returnCatalogueUnitBean.getUnitid())) {
                            catalogueUnitBean.setSelected(true);
                            mShowingCatalogueUnitBeans.put(catalogueUnitBean.getUnitid(), catalogueUnitBean);
                        } else {
                            catalogueUnitBean.setSelected(false);
                            mShowingCatalogueUnitBeans.remove(catalogueUnitBean.getUnitid());
                        }
                    }
                }

            }
        } catch (Exception e) {

        }

        return catalogueUnitBeanList;
    }

    //查找选中的CataloguesUnitBean
    private CatalogueUnitBean getSelectedCataloguesUnitBean(List<CatalogueUnitBean> catalogueUnitBeanList) {
        try {
            if (catalogueUnitBeanList != null && catalogueUnitBeanList.size() > 0) {
                for (int i = 0; i < catalogueUnitBeanList.size(); i++) {
                    CatalogueUnitBean catalogueUnitBean = catalogueUnitBeanList.get(i);
                    if (catalogueUnitBean.getChildren() != null && catalogueUnitBean.getChildren().size() > 0) {
                        CatalogueUnitBean selectedCataloguesUnitBean = getSelectedCataloguesUnitBean(catalogueUnitBean.getChildren());
                        if (selectedCataloguesUnitBean != null) {
                            return selectedCataloguesUnitBean;
                        }
                    } else {
                        if (catalogueUnitBean.isSelected()) {
                            return catalogueUnitBean;
                        }
                    }
                }

            }
        } catch (Exception e) {

        }

        return null;

    }

    //默认选中第一个可用单元的第一个最后层级
    private List<CatalogueUnitBean> getLastLevelCataloguesUnitBean(List<CatalogueUnitBean> catalogueUnitBeanList) {
        try {
            if (catalogueUnitBeanList != null && catalogueUnitBeanList.size() > 0) {
                CatalogueUnitBean catalogueUnitBean = catalogueUnitBeanList.get(0);
                if (catalogueUnitBean.getChildren() != null && catalogueUnitBean.getChildren().size() > 0) {
                    catalogueUnitBean.setChildren(getLastLevelCataloguesUnitBean(catalogueUnitBean.getChildren()));
                } else {
                    catalogueUnitBean.setSelected(true);
                    mReadyShowCatalogueUnitBeans.put(catalogueUnitBean.getUnitid(), catalogueUnitBean);
                }
            }
        } catch (Exception e) {

        }

        return catalogueUnitBeanList;
    }

    //同步本地选中的记录到刷新回来的数据中
    private void synCataloguesBean(BookDeskCataloguesBean bookDeskCatalogueBean) {
        if (bookDeskCatalogueBean == null) {
            return;
        }
        try {
            mReadyShowCatalogueUnitBeans.clear();
            List<BookDeskCataloguesBean.CataloguesBean> netCatalogues = bookDeskCatalogueBean.getCatalogues();
            if (netCatalogues != null) {
                boolean isSelected = false;
                for (int i = 0; i < netCatalogues.size(); i++) {
                    isSelected = false;
                    BookDeskCataloguesBean.CataloguesBean netCataloguesBean = netCatalogues.get(i);
                    if (netCataloguesBean.getData() != null) {
                        isSelected = synSelectedCatalogueUnitBean(netCataloguesBean.getData());
                        //默认选中第一个
                        if (!isSelected && netCataloguesBean.getData().size() > 0) {
                            getLastLevelCataloguesUnitBean(netCataloguesBean.getData());
                        }

                    }
                }
                mCataloguesBeanList = netCatalogues;
                mBookDeskCatalogueBean = bookDeskCatalogueBean;
                mShowingCatalogueUnitBeans.clear();
                mShowingCatalogueUnitBeans.putAll(mReadyShowCatalogueUnitBeans);
            }
        } catch (Exception e) {

        }


    }

    private void updateCataloguesBeanFromNet(BookDeskCataloguesBean bookDeskCataloguesBean) {
        synCataloguesBean(bookDeskCataloguesBean);
        updateCataloguesAdapterAndSaveLocal();
    }

    private void updateCataloguesAdapterAndSaveLocal() {
        mNewBookDeskAdapter.updateCatalogueList(mCataloguesBeanList);
        // 保存新书桌首页目录数据到本地
        SerializableManager.getInstance().serialize(SerializableManager.SerializeKey.SHARE_KEY_BOOK_DESK_CATALOGUE, (Serializable) mBookDeskCatalogueBean);
    }


    //单个item目录刷新
    private void getBookDeskItemCatalogue(String vesionIds, final int subjectPosition, final boolean isSwitchReresh, final MaterialBean materialBean) {
        if (TextUtils.isEmpty(vesionIds)) {
            return;
        }
        if (mContext instanceof BaseActivity) {
            BaseActivity baseActivity = (BaseActivity) mContext;
            baseActivity.showCricleProgress();
        }

        EpaperDao.getInstance().getBookDeskCatalogue(vesionIds, new BaseExtCallBack(mContext) {
            @Override
            public void failed(int errorCode, Object data) {
                super.failed(errorCode, data);
                if (mContext instanceof BaseActivity) {
                    BaseActivity baseActivity = (BaseActivity) mContext;
                    baseActivity.hideCricleProgress();
                }
//				hideCricleProgress();
                if (null != data && errorCode != 17 && errorCode != 27) {
                    ToastUtil.INSTANCE.toastCenterError(R.string.server_error);
                }
            }

            @Override
            public void failed(Object data) {
                super.failed(data);
                if (mContext instanceof BaseActivity) {
                    BaseActivity baseActivity = (BaseActivity) mContext;
                    baseActivity.hideCricleProgress();
                }
                if (!NetworkUtils.isOnline()) {
                    ToastUtil.INSTANCE.toastCenterNoNetError();
                } else if (data instanceof TimeoutError) {
                    ToastUtil.INSTANCE.toastCenterError("连接超时，请稍后重试");
                }
            }

            @Override
            public void success(Object data) {
                if (mContext instanceof BaseActivity) {
                    BaseActivity baseActivity = (BaseActivity) mContext;
                    baseActivity.hideCricleProgress();
                }
                try {
                    if (data != null && data instanceof BookDeskCataloguesBean) {
                        BookDeskCataloguesBean.CataloguesBean netCataloguesBean = ((BookDeskCataloguesBean) data).getCatalogues().get(0);
                        boolean isSelected = false;
                        isSelected = synSelectedItemCatalogueUnitBean(netCataloguesBean.getData());
                        //默认选中第一个
                        if (!isSelected && netCataloguesBean.getData().size() > 0) {
                            getLastLevelCataloguesUnitBean(netCataloguesBean.getData());
                        }
                        mCataloguesBeanList.set(subjectPosition, netCataloguesBean);
                        updateCataloguesAdapterAndSaveLocal();

                        if (isSwitchReresh) {
                            List<MaterialBean> materialBeanList = mSubjectBeanList.get(subjectPosition).getMaterial();
                            for (int i = 0; i < materialBeanList.size(); i++) {
                                if (materialBean.getVersionId() == materialBeanList.get(i).getVersionId()) {
                                    materialBeanList.get(i).setSelected(true);
                                    if (mVersionIdList != null && mVersionIdList.size() > subjectPosition) {
                                        mVersionIdList.set(subjectPosition, materialBean.getVersionId() + "");
                                    }
                                } else {
                                    materialBeanList.get(i).setSelected(false);
                                }
                            }
                            updateSubjectBeanListAdapterAndSaveLocal();
                        }
                    } else {
                        ToastUtil.INSTANCE.toastCenterError(R.string.server_error);
                    }
                } catch (Exception e) {

                }


            }
        });
    }

    //整个页面目录刷新
    private void getBookDeskCatalogue(String vesionIds) {
        if (TextUtils.isEmpty(vesionIds)) {
            return;
        }

        EpaperDao.getInstance().getBookDeskCatalogue(vesionIds, new BaseExtCallBack(getContext()) {
            @Override
            public void failed(int errorCode, Object data) {
                super.failed(errorCode, data);

            }

            @Override
            public void failed(Object data) {
                super.failed(data);
            }

            @Override
            public void success(Object data) {
                if (data != null && data instanceof BookDeskCataloguesBean) {
                    BookDeskCataloguesBean bookDeskCataloguesBean = (BookDeskCataloguesBean) data;
                    updateCataloguesBeanFromNet(bookDeskCataloguesBean);
                }

            }
        });
    }


    private void initDialogFragment() {
        if (chackGradeBottomDialog == null)
            chackGradeBottomDialog = new ChackGradeBottomDialog(getContext(), R.style.DialogStyleGuide);
        chackGradeBottomDialog.setCanceledOnTouchOutside(true);
        if (catalogBottomDialog == null)
            catalogBottomDialog = new CatalogBottomDialog(getContext(), R.style.DialogStyleGuide);
        catalogBottomDialog.setCanceledOnTouchOutside(true);


    }


    private String getVersionIds(ArrayList<String> versionId) {
        if (versionId != null && versionId.size() > 0) {
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < versionId.size(); i++) {
                if (i == versionId.size() - 1) {
                    stringBuilder.append(versionId.get(i));
                } else {
                    stringBuilder.append(versionId.get(i) + ",");
                }

            }
            return stringBuilder.toString();
        } else {
            return "";
        }

    }

    private String getBookIds(List<MaterialBean> materialBeanList) {

        try {
            StringBuffer mBookId = new StringBuffer();
            for (MaterialBean materialBean : materialBeanList) {
                if (materialBean.isSelected()) {
                    List<MaterialBean.BooksBean> books = materialBean.getBooks();
                    for (int i = 0; i < books.size(); i++) {
                        MaterialBean.BooksBean booksBean = books.get(i);
                        if (i == books.size() - 1) {
                            mBookId.append(booksBean.getBookId());
                        } else {
                            mBookId.append(booksBean.getBookId() + ",");
                        }
                    }

                }
            }
            return mBookId.toString();
        } catch (Exception e) {

        }

        return "";
    }

    private MaterialBean getMaterialBean(List<MaterialBean> materialBeanList) {

        MaterialBean mMaterialBean = null;
        for (MaterialBean materialBean : materialBeanList) {
            if (materialBean.isSelected()) {
                mMaterialBean = materialBean;
                break;
            }
        }
        return mMaterialBean;
    }

    private void updateSubjectBeanListAdapterAndSaveLocal() {
//		mHandler.sendEmptyMessage(UPDATE_SUBJECT_LIST);
        mNewBookDeskAdapter.updateSubjectBeanList(mSubjectBeanList);
        // 保存新书桌首页数据到本地
        SerializableManager.getInstance().serialize(SerializableManager.SerializeKey.SHARE_KEY_BOOK_DESK_HOME_PAGE, (Serializable) mBookDeskBean);
    }

    public void bindData(BookDeskBean bookDeskBean, List<SubjectBean> subjectBeanList, boolean isLocal) {
        mBookDeskBean = bookDeskBean;
        if (isLocal) {
            updateMaterialItemBeansLocal(subjectBeanList);
            EventBus.getDefault().post(new RefreshNewBookDesk(true));
            Log.e(TAG, "bindData:isLocal " + isLocal);
        } else {
            Log.e(TAG, "bindData:isLocal " + isLocal);
            updateMaterialItemBeansFromNet(subjectBeanList);
            getBookDeskCatalogue(getVersionIds(mVersionIdList));
        }

    }
}
